package com.vend.common.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.github.yuweiguocn.library.greendao.MigrationHelper;
import com.vend.common.BuildConfig;

import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.query.QueryBuilder;

/**
 * 1、创建数据库
 * 2、创建数据库的表
 * 3、包含对数据库的CRUD
 * 4、对数据库的升级
 * Created by jiadong on 2016/11/7.
 */

public class GreenDAOHelper {
    private static final String TAG = GreenDAOHelper.class.getSimpleName();
    private volatile static GreenDAOHelper sManager;//多线程访问
    private static DaoMaster.DevOpenHelper helper;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private String dbName = DBConfig.DB_NAME;//数据库名称
    private Context context;

    /**
     * 使用单例模式获得操作数据库的对象
     *
     * @return
     */
    public static GreenDAOHelper getInstance() {
        GreenDAOHelper instance = null;
        if (sManager == null) {
            synchronized (GreenDAOHelper.class) {
                if (instance == null) {
                    instance = new GreenDAOHelper();
                    sManager = instance;
                }
            }
        }
        return sManager;
    }

    /**
     * 初始化操作
     *
     * @param context
     * @param dbName
     */
    public void init(Context context, String dbName) {
        this.context = context;
        this.dbName = dbName;
    }

    /**
     * 判断是否存在数据库，如果没有则创建数据库
     *
     * @return
     */
    private synchronized DaoMaster getDaoMaster() {
        GSTOpenHelper helper = new GSTOpenHelper(new GreenDaoContext(context), dbName, null);
        //用于第三方库库数据库调试
        if (BuildConfig.DEBUG) {
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        } else {
            daoMaster = new DaoMaster(helper.getEncryptedWritableDb(DBConfig.DB_ENCRYPT_PWD));
        }
        return daoMaster;
    }
    public DaoSession getDaoSession() {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster();
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    /**
     * 打开输出日志的操作,默认是关闭的
     */
    public void setDebug() {
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
    }

    /**
     * 关闭所有的操作,数据库开启的时候，使用完毕了必须要关闭
     */
    public void closeConnection() {
        closeHelper();
        closeDaoSession();
    }

    private void closeHelper() {
        if (helper != null) {
            helper.close();
            helper = null;
        }
    }

    private void closeDaoSession() {
        if (daoSession != null) {
            daoSession.clear();
            daoSession = null;
        }
    }

    private class GSTOpenHelper extends DaoMaster.OpenHelper {
        GSTOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory);
        }

        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
                @Override
                public void onCreateAllTables(Database db, boolean ifNotExists) {
                    DaoMaster.createAllTables(db, ifNotExists);
                }

                @Override
                public void onDropAllTables(Database db, boolean ifExists) {
                    DaoMaster.dropAllTables(db, ifExists);
                }
            }, UserDbEntityDao.class);
        }
    }
}

